//
//  ViewController.m
//  定位：WhereAmI
//
//  Created by student on 16/4/1.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Place.h"
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *longtitudeLable;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;

//位置管理器
@property(nonatomic,strong)CLLocationManager *locationManager;

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)btnPressed:(id)sender
{
        
    self.locationManager = [[CLLocationManager alloc] init];
    
    // 定位精度设置(精度越高，耗电越多)
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 代理人
    self.locationManager.delegate = self;
    // 更新位置距离设定()
    self.locationManager.distanceFilter = 100.0f;// 在100米范围内移动，不会刷新位置
    // 请求用户给与访问位置信息的权限
    [self.locationManager requestWhenInUseAuthorization];
    // 启动定位
    [self.locationManager startUpdatingLocation];
}

//旧方法
//- (void)locationManager:(CLLocationManager *)manager
//    didUpdateToLocation:(nonnull CLLocation *)newLocation
//           fromLocation:(nonnull CLLocation *)oldLocation
//{
//    // 1 将得到的经纬度显示到界面
//    self.longtitudeLable.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.longitude];
//    self.latitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.latitude];
//    
//    // 2. 如果不需要时及时关闭定位的更新(减少耗电)
//    [self.locationManager stopUpdatingLocation];
//}


//新方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    CLLocation *newLocation=[locations lastObject];
    self.longtitudeLable.text=[NSString stringWithFormat:@"%.5f",newLocation.coordinate.longitude];
    self.latitudeLabel.text = [NSString stringWithFormat:@"%.5f",newLocation.coordinate.latitude];
    NSLog(@"%.5f,%.5f",newLocation.coordinate.longitude,newLocation.coordinate.latitude);
    
    //设定地图
    //范围大小
    MKCoordinateSpan span={0.01,0.01};
    //区域
    MKCoordinateRegion regin={newLocation.coordinate,span};
    [self.mapView setRegion:regin];
    
    //根据经纬度信息-》位置信息
    CLGeocoder *geoCoder=[[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *place=[placemarks lastObject];
        NSLog(@"位置名称：%@",place.name);
        NSLog(@"位置街道：%@",place.thoroughfare);
        NSLog(@"位置区:%@",place.subLocality);
        NSLog(@"位置城市:%@",place.locality);
        NSLog(@"位置国家:%@",place.country);
    }];
    
    //添加标注
    Place *myPlace=[[Place alloc]init];
    myPlace.coordinate=newLocation.coordinate;
    myPlace.title=@"我的位置";
    myPlace.subtitle=@"正则路18号";
    self.mapView.delegate=self;
    [self.mapView addAnnotation:myPlace];
    
    // 2. 如果不需要时及时关闭定位的更新(减少耗电)
    [self.locationManager stopUpdatingLocation];
    
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"Anntotaion"];
    if(pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Anntotaion"];
    }
    
    pinView.pinTintColor = [UIColor blueColor];
    //    pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;//显示弹出信息
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    btn.tag = 101;
    [btn addTarget:self action:@selector(pinBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = btn;
    
    return pinView;
}

- (void)pinBtnPressed:(UIButton *)btn
{
    NSLog(@"%s",__func__);
    [self performSegueWithIdentifier:@"gotoDetailInfoVC" sender:nil];
}


@end
